﻿
namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Security.Principal;
    using System.Threading;
    using System.Web;
    using Spring.Context;
    using Spring.Context.Support;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Core.Extension;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Core.Message;

    public class CurrentUserProvider 
    {
        const string Viewer = "游客";
        private IApplicationContext ctx;
        private IEmployeeAppService EmployeeAppService;
        private ICorporationAppService CorporationAppService;
        private IUserAccountAppService UserAccountAppService;

        public CurrentUserProvider()
        {
            Init();
        }

        public virtual UserAccount UserAccount
        {
            get { return GetUserAccount(); }
        }

        public virtual Corporation Corporation
        {
            get { return GetCorporation(); }
        }

        public virtual Role UserRole
        {
            get { return GetUserRole(); }
        }

        #region protected User
        protected virtual UserAccount GetUserAccount()
        {
            //UserAccount userAccount = null;
            //userAccount = HttpContext.Current.Session()[GetType().FullName + "UserAccount"] as UserAccount; // is cached for this request
            //if (Identity.IsAuthenticated)
            //{
            //    if (userAccount.IsNull())
            //    {
            //        UserAccountSearch userAccountSearch = new UserAccountSearch();
            //        userAccountSearch.Name = Identity.Name;
            //        userAccount = UserAccountAppService.GetSingleBySearch(userAccountSearch);

            //        HttpContext.Current.Session()[GetType().FullName + "UserAccount"] = userAccount; // keep it until end of the request
            //    }
            //    return userAccount;
            //}

            //if (userAccount.IsNull())
            //{
            //    userAccount = UserAccountFactory.CreateUserAccount(Viewer);
            //    HttpContext.Current.Session()[GetType().FullName + "UserAccount"] = userAccount;
            //    // keep it until end of the request
            //}
            //return userAccount;

            var userAccount = UserAccountAppService.GetById(1);

            return userAccount;
        }

        protected virtual Corporation GetCorporation()
        {
            if (Identity.IsAuthenticated)
            {
                Corporation currentCorporation = new Corporation();
                Corporation corporation = HttpContext.Current.Items[GetType().FullName + "CurrentCorporation"] as Corporation; // is cached for this request
                if (corporation.IsNull())
                {
                    this.EmployeeAppService = this.ctx.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
                    this.CorporationAppService = this.ctx.GetObject(SystemModuleConst.CORPORATIONAPPSERVICE) as ICorporationAppService;
                    Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
                    if (employee != null)
                    {
                        corporation = this.CorporationAppService.GetById(employee.Corporation.Id);
                    }
                    if (corporation.IsNotNull())
                    {
                        currentCorporation.Id = corporation.Id;
                    }
                    HttpContext.Current.Items[GetType().FullName + "CurrentCorporation"] = currentCorporation; // keep it until end of the request
                }
                return currentCorporation;
            }
            else
            {
                Corporation currentCorporation = new Corporation();
                currentCorporation.Id = "001";
                return currentCorporation;
            }

        }

        protected virtual Role GetUserRole()
        {
            Role role = null;
            if (Identity.IsAuthenticated)
            {
                role = HttpContext.Current.Session()[GetType().FullName + "Role"] as Role; // is cached for this request
            }
            if (role.IsNull())
            {
                role = new Role(Huirui.Cavan.Domain.MainBoundedContext.SystemModule.SystemIdentifier.Role.SystemAdministrator.GetHashCode(), true);
                HttpContext.Current.Session()[GetType().FullName + "Role"] = role;
            }
            return role;
        }
        #endregion protected User

        #region protected properties
        protected virtual IIdentity Identity
        {
            get { return Thread.CurrentPrincipal.Identity; }
        }
        #endregion protected properties

        #region IoC
        

        void Init()
        {
            ctx = ContextRegistry.GetContext();
            this.UserAccountAppService = ctx.GetObject(SystemModuleConst.USERACCOUNTAPPSERVICE) as IUserAccountAppService;
            IList<AppMessage> lstMessage = new List<AppMessage>();
            UserAccountAppService.Messages = lstMessage;
        }
        #endregion IoC
    }
}